﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.BLL;
using QLHD.DAL;

namespace QLHD
{
    public partial class frmPhanLoai : Form
    {
        string maauto;
        public frmPhanLoai() { }
        public frmPhanLoai(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmPhanLoai_Load(object sender, EventArgs e)
        {
            dgvPhanLoai.DataSource = PhanLoai_DB.SelectAll();
            ReadOnlyTextbox();
            LoadCombobox();
            DisplayData(0);
            
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (btnThem.Text == "Tạo mới")
            {
                btnThem.Text = "Lưu";
                dtpNgayThang.Text = DateTime.Now.ToString();
                AutoMaCT_PL();
                txtMaCT_PL.Text = maauto;
                PrepareCombobox();
                NotReadOnlyTextbox();
                ClearTextbox();

            }
            else
            {
                try
                {
                    InsertCT_PL();
                    btnThem.Text = "Tạo mới";
                    frmPhanLoai_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDienGiai.Focus();
                }
            }         
        }

        private void InsertCT_PL()
        {
            PhanLoai item = new PhanLoai();
            item.MaCT_PL = txtMaCT_PL.Text;
            item.Ngaythang = dtpNgayThang.Text;
            item.MaNX = cbbMaNX.SelectedValue.ToString();
            item.MaLo = cbbMaLo.SelectedValue.ToString();
            item.MaTP = cbbMaTP.SelectedValue.ToString();
            item.DienGiai = txtDienGiai.Text;
            item.KL_NhapPL =double.Parse(txtNhapPL.Text);
            item.KL_XuatTP = double.Parse(txtXuatTP.Text);
            PhanLoai_DB.Insert(item);
        }
        void LoadCombobox()
        {
            cbbMaLo.DataSource = Lo_DB.SelectLo();
            cbbMaLo.DisplayMember = "diengiai";
            cbbMaLo.ValueMember = "malo";

            cbbMaTP.DataSource = MaTP_DB.SelectMaTP_PhanLoai();
            cbbMaTP.DisplayMember = "matp";
            cbbMaTP.ValueMember = "matp";

            cbbMaNX.DataSource = MaNX_DB.SelectAllMaNX();
            cbbMaNX.DisplayMember = "manx";
            cbbMaNX.ValueMember = "manx";
        }

        private void AutoMaCT_PL()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 MaCT_PhanLoai from tblCT_PhanLoai where NgayThang = '" + dtpNgayThang.Text + "' order by MaCT_PhanLoai desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);

            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayThang.Text.Replace("/", "") + "1";
            }
            else
            {
                maauto = (Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
            }
        }
        private void ReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = true;
            txtNhapPL.ReadOnly = true;
            txtXuatTP.ReadOnly = true;
        }
        private void NotReadOnlyTextbox()
        {
            txtDienGiai.ReadOnly = false;
            txtNhapPL.ReadOnly = false;
            txtXuatTP.ReadOnly = false;
        }

        private void ClearTextbox()
        {
            txtDienGiai.Clear();
            txtNhapPL.Clear();
            txtXuatTP.Clear();
        }

        private void PrepareCombobox()
        {
            cbbMaLo.Text = "--Chọn mã lô--";
            cbbMaNX.Text = "--Chọn mã N/X";
            cbbMaTP.Text = "--Chọn mã TP";
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (btnSua.Text == "Cập nhật")
            {
                btnSua.Text = "Lưu";
                NotReadOnlyTextbox();
                
            }
            else
            {
                try
                {
                    PhanLoai item = new PhanLoai();
                    item.MaCT_PL = txtMaCT_PL.Text;
                    item.Ngaythang = dtpNgayThang.Text;
                    item.MaNX = cbbMaNX.SelectedValue.ToString();
                    item.MaLo = cbbMaLo.SelectedValue.ToString();
                    item.MaTP = cbbMaTP.SelectedValue.ToString();
                    item.DienGiai = txtDienGiai.Text;
                    item.KL_NhapPL = double.Parse(txtNhapPL.Text);
                    item.KL_XuatTP = double.Parse(txtXuatTP.Text);
                    PhanLoai_DB.Update(txtMaCT_PL.Text, item);
                    
                    btnSua.Text = "Cập nhật";
                    frmPhanLoai_Load(sender, e);
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDienGiai.Focus();
                }
            }         
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {

                PhanLoai_DB.Delete(txtMaCT_PL.Text);
                frmPhanLoai_Load(sender, e);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmPhanLoai_Load(sender, e);
            btnThem.Text = "Thêm";
            btnSua.Text = "Sửa";
        }
        private void DisplayData(int index)
        {
            try
            {
                txtMaCT_PL.Text = dgvPhanLoai.Rows[index].Cells[0].Value.ToString();
                dtpNgayThang.Text = dgvPhanLoai.Rows[index].Cells[1].Value.ToString();
                cbbMaNX.Text = dgvPhanLoai.Rows[index].Cells[2].Value.ToString();
                cbbMaLo.Text = dgvPhanLoai.Rows[index].Cells[3].Value.ToString();
                cbbMaTP.Text = dgvPhanLoai.Rows[index].Cells[4].Value.ToString();
                txtDienGiai.Text = dgvPhanLoai.Rows[index].Cells[5].Value.ToString();
                txtNhapPL.Text = dgvPhanLoai.Rows[index].Cells[6].Value.ToString();
                txtXuatTP.Text = dgvPhanLoai.Rows[index].Cells[7].Value.ToString();
                
            }
            catch (Exception)
            {
                return;
            }

        }

        private void dgvPhanLoai_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DisplayData(e.RowIndex);
        }
    }
}